package org.example.bookshelf_manager.controller;

import jakarta.servlet.http.HttpSession;
import org.example.bookshelf_manager.constant.Myconstants;
import org.example.bookshelf_manager.model.Result;
import org.example.bookshelf_manager.model.UserInfo;
import org.example.bookshelf_manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String userName , String password, HttpSession session){
//        1.校验参数
        if (!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return "用户名或密码为空! ";
        }

        //        2.验证密码是否正确

//        根据用户名从数据库查询用户信息 如果未查询到则没有此用户
//        查询到了 校验密码是否正确
        UserInfo userInfoByName = userService.getUserInfoByName(userName);
        if (userInfoByName == null){
            return "用户不存在";
        }
        if (!password.equals(userInfoByName.getPassword())){
            return "密码错误";
        }
//
//        正确的情况
        session.setAttribute("user_session_key",userInfoByName);
        UserInfo logUserInfo = (UserInfo)session.getAttribute(Myconstants.USER_SESSION_KEY);
        System.out.println(); //
        return "";
    }
}
// controller 层 调用 service 用于处理业务       service 层 调用 mapper层 用于操作数据库